<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/7/2
 * Time: 9:56
 */

namespace app\commi\controller;

use think\Db;
use think\Request;

class Set extends Base
{
    /**
     * 终点时限
     * @return mixed|\think\response\Json
     */
    public function zhongLimit(){
        if(Request::instance()->isPost()){
            $match = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->field('match_id, match_cate')
                ->find();
            if(!$match){
                return json(['code' => 100, 'msg' => '请先创建赛事']);
            }
            $match_zhong_limit = trim(input('match_zhong_limit'), ' ');
            $res = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->setField('match_zhong_limit', $match_zhong_limit);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            $zhong = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->value('match_zhong_limit');
            return $this->fetch('set/zhonglimit', ['zhong' => $zhong]);
        }
    }
    
    /**
     * 封闭时限
     * @return mixed|\think\response\Json
     */
    public function fengLimit(){
        if(Request::instance()->isPost()){
            $match = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->field('match_id, match_cate')
                ->find();
            if(!$match){
                return json(['code' => 100, 'msg' => '请先创建赛事']);
            }
            $match_feng_limit = trim(input('match_feng_limit'), ' ');
            $res = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->setField('match_feng_limit', $match_feng_limit);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            $feng = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->value('match_feng_limit');
            return $this->fetch('set/fenglimit', ['feng' => $feng]);
        }
    }
    
    /**
     * 1标封闭时限
     * @return mixed|\think\response\Json
     */
    public function firstFengLimit(){
        if(Request::instance()->isPost()){
            $match = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->field('match_id, match_cate')
                ->find();
            if(!$match){
                return json(['code' => 100, 'msg' => '请先创建赛事']);
            }
            $match_first_feng_limit = trim(input('match_first_feng_limit'), ' ');
            $res = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->setField('match_first_feng_limit', $match_first_feng_limit);
            if($res){
                return json(['code' => 200, 'msg' => '保存成功']);
            }else{
                return json(['code' => 100, 'msg' => '保存失败']);
            }
        }
        $lose = $this->lostTime();
        if($lose == 1){
            return $this->fetch('/lose');
        }else{
            $firstFeng = Db::table('up_match')
                ->where('match_committee_id', session('com_id'))
                ->where('match_member_id', session('cm_id'))
                ->value('match_first_feng_limit');
            return $this->fetch('set/firstfenglimit', ['firstFeng' => $firstFeng]);
        }
    }
}